package com.neutech.maplestory.entity;

import com.neutech.maplestory.client.MapleStoryClient;
import com.neutech.maplestory.constant.Constant;
import com.neutech.maplestory.util.ImageUtil;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.List;

/**
 * @author Neuedu-远哥
 * @version 1.0
 * @date 2021/11/29 9:10
 */
public class Hero extends AbstractMapleStoryObject {
    /**
     * 英雄左站的数组
     */ {
        standImages = new Image[4];
        for (int i = 0; i < standImages.length; i++) {
            standImages[i] = ImageUtil.getImageByKey("hero_stand" + i);
        }
        walkImages = new Image[5];
        for (int i = 0; i < walkImages.length; i++) {
            walkImages[i] = ImageUtil.getImageByKey("hero_walk" + i);
        }
        shootImages = new Image[4];
        for (int i = 0; i < shootImages.length; i++) {
            shootImages[i] = ImageUtil.getImageByKey("hero_shoot" + i);
        }
        // 跳
        jumpImages = new Image[1];
        jumpImages[0] = ImageUtil.getImageByKey("hero_jump");
        // 趴下
        proneImages = new Image[1];
        proneImages[0] = ImageUtil.getImageByKey("hero_prone");

    }

    /**
     * 人物的攻击力
     */
    public int attack;

    /**
     * 英雄的各个状态的开关
     */
    public boolean left, right, prone, walk, shoot , pickup;

    public Hero() {
        this.speed = Constant.HERO_SPEED;
        this.dir = Direction.RIGHT;
        this.action = Action.STAND;
        this.width = standImages[0].getWidth(null);
        this.height = standImages[0].getHeight(null);
        this.attack = 1209;
    }

    public Hero(MapleStoryClient msc,int x, int y) {
        this();
        this.msc = msc;
        this.x = x;
        this.y = y;
    }




    @Override
    public void draw(Graphics g) {
        // 避免出现int类型的最大（溢出）
        if (rate >= Integer.MAX_VALUE) {
            rate = 0;
        }
        if (standStep > 3) {
            standStep = 0;
        }
        if (walkStep > 4) {
            walkStep = 0;
        }
        if (shootStep > 3) {
            shootStep = 0;
        }

        // 方向的判断
        confirmDirection();
        switch (dir) {
            case LEFT:
                switch (action) {
                    case STAND:
                        g.drawImage(standImages[standStep], x, y, null);
                        break;
                    case WALK:
                        g.drawImage(walkImages[walkStep], x, y, null);
                        break;
                    case SHOOT:
                        g.drawImage(shootImages[shootStep], x, y, null);
                        break;
                    case JUMP:
                        g.drawImage(jumpImages[0], x, y, null);
                        break;
                    case PRONE:
                        g.drawImage(proneImages[0], x, y + 26, null);
                        break;
                    default:
                        break;
                }

                break;
            case RIGHT:
                // 转成2D带有transform属性的对象
                Graphics2D graphics2D = (Graphics2D) g;
                switch (action) {
                    case STAND:
                        graphics2D.drawImage(standImages[standStep], ImageUtil.flipHorizontal(standImages[standStep], x, y), null);
                        break;
                    case WALK:
                        graphics2D.drawImage(walkImages[walkStep], ImageUtil.flipHorizontal(walkImages[walkStep], x, y), null);
                        break;
                    case SHOOT:
                        graphics2D.drawImage(shootImages[shootStep], ImageUtil.flipHorizontal(shootImages[shootStep], x, y), null);
                        break;
                    case JUMP:
                        graphics2D.drawImage(jumpImages[0], ImageUtil.flipHorizontal(jumpImages[0], x, y), null);
                        break;
                    case PRONE:
                        graphics2D.drawImage(proneImages[0], ImageUtil.flipHorizontal(proneImages[0], x, y + 26), null);
                        break;
                    default:
                        break;
                }
                break;
            default:
                break;

        }
        if (rate++ % 5 == 0) {
            standStep++;
            walkStep++;
            shootStep++;
        }

        // 人物的矩形
        g.drawRect(x + 30,y,width - 50,height);

        move();
    }

    /**
     * 根据boolean类型的状态确定方向
     */
    private void confirmDirection() {
        if (left) {
            this.dir = Direction.LEFT;
        }
        if (right) {
            this.dir = Direction.RIGHT;
        }
    }

    @Override
    public void move() {
        if (left && !right) {
            this.x -= this.speed;
        }
        if (right && !left) {
            this.x += this.speed;
        }

        if (prone) {
            this.action = Action.PRONE;
        } else if (walk) {
            this.action = Action.WALK;
        } else if (shoot) {
            shoot();
            this.action = Action.SHOOT;
        } else {
            this.action = Action.STAND;
        }


        if (jump) {
            this.action = Action.JUMP;
            jump(msc.grounds);
        }else{
            jumpDown(msc.grounds);
        }
        // 判断英雄出界
        outOfBounds();
    }
    private int shootCount;
    private void shoot(){
        shootCount++;
        if(shootCount % 10 == 0){
            // 创建一个弓箭对象
            Arrow arrow;
            if(this.dir == Direction.LEFT){
                arrow = new Arrow(msc,this.x,this.y + 48, this.dir);
            }else{
                arrow = new Arrow(msc,this.x + this.width,this.y + 48, this.dir);
            }
            msc.arrows.add(arrow);
            shoot = false;
        }
    }

    /**
     * 判断英雄出界
     */
    private boolean outOfBounds() {
        if (this.x <= 0) {
            this.x = 0;
            return true;
        }
        if (this.x >= Constant.WINDOW_WIDTH - this.width) {
            this.x = Constant.WINDOW_WIDTH - this.width;
            return true;
        }
        // 判断地图内不可以穿过的墙
        if (this.dir == Direction.RIGHT) {
            /*if (x >= 988 && y > 638) {
                x = 988;
            }
            if (x >= 1078 && y > 578 && x < 1498) {
                x = 1078;
            }*/
            if (x >= 1040 && y > 638) {
                x = 1040;
            }
            if (x >= 1130 && y > 578 && x < 1540) {
                x = 1130;
            }
        }
        if (this.dir == Direction.LEFT) {
            if (x <= 1600 && y > 578 && x > 1160) {
                x = 1600;
            }
        }

        return false;
    }

    /**
     * 英雄按键按下的方法
     *
     * @param e 键盘事件对象
     */
    public void keyPressed(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_A:
                left = true;
                walk = true;
                break;
            case KeyEvent.VK_D:
                right = true;
                walk = true;
                break;
            case KeyEvent.VK_S:
                prone = true;
                break;
            case KeyEvent.VK_K:
                jump = true;
                break;
            case KeyEvent.VK_J:
                shoot = true;
                break;
            case KeyEvent.VK_L:
                pickup = true;
                break;
            case KeyEvent.VK_I:
                msc.itemPackage.live = !msc.itemPackage.live;
                break;
            default:
                break;
        }
    }

    /**
     * 英雄按键抬起的方法
     * @param e 键盘事件对象
     */
    public void keyReleased(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_A:
                left = false;
                walk = false;
                break;
            case KeyEvent.VK_D:
                right = false;
                walk = false;
                break;
            case KeyEvent.VK_S:
                prone = false;
                break;
            case KeyEvent.VK_L:
                pickup = false;
                break;
            default:
                break;
        }
    }




    @Override
    public Rectangle getRectangle() {
        return new Rectangle(x+30,y,width-50,height);
    }


    private boolean pickup(Item item){
        if(pickup && this.getRectangle().intersects(item.getRectangle())){
            // 死掉
            item.live = false;
            // 判断是否是同一种道具，是：合并
            for(int i=0;i<msc.itemPackage.items.size();i++){
                Item it = msc.itemPackage.items.get(i);
                if(it.type == item.type){
                    it.qty += 1;
                    return true;
                }
            }
            // 向道具包中添加
            msc.itemPackage.items.add(item);

            return true;
        }
        return false;
    }

    public boolean pickup(List<Item> items){
        for (int i = 0; i < items.size(); i++) {
            Item item = items.get(i);
            if(pickup(item)){
                return true;
            }
        }
        return false;
    }
}
